/********************************************************************************************/
/*																							*/
/*							Twn_MastTracker.cpp												*/
/*																							*/
/*	Description: library contains a function that manages the runtime mimics' update for the*/
/*				 Tianwan NPP FHM CCS graphical pages. The actions performed are automatic	*/
/*				 page change when the FHM is moving (both X,Y and Z movements), and run-time*/
/*				 trajectory update in the Full Map mimic.									*/
/*																							*/
/********************************************************************************************/

#define ALONE_TWN_MAST_TRACK
#include "Twn_MastTracker.h"

int			sampleTime;
bool		testPool;
bool		disabled;
TCHAR		testPoolPage[32];

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved){
	LONG		res;
	LPDWORD		type;
	LPBYTE		data;
	LPDWORD		size;
	HKEY		hKey;
    DWORD		dwBufLen = MY_BUFSIZE;
	DWORD		active;
    TCHAR		regStr[32];
	size = 0;
	data = 0;
	type = 0;
	
	
	switch(fdwReason){ 
		case DLL_PROCESS_ATTACH:
			RegOpenKeyEx(	HKEY_LOCAL_MACHINE, 
							TEXT("SOFTWARE\\ABB\\Power Generation Portal\\Tianwan\\Twn_MastTracker"),
							0,
							KEY_QUERY_VALUE,
							&hKey);
			active = 0;
			dwBufLen = sizeof(active);
			res = RegQueryValueEx(	hKey,
									TEXT("SampleTime"),
									NULL,
									NULL,
									(LPBYTE)&active,
									&dwBufLen);
			if(res == ERROR_SUCCESS)
				sampleTime = (int)active;
			else{
				MessageBox(NULL, _T("Twn_MastTracker.dll failed initialization.\nRegisry key \"SOFTWARE\\ABB\\Power Generation Portal\\Tianwan\\Twn_MastTracker\\SampleTime\" ot found."), _T("Error"), MB_OK | MB_ICONERROR);
				return(FALSE);
			}


			dwBufLen = sizeof(regStr);
			res = RegQueryValueEx(	hKey,
									TEXT("TestPool"),
									NULL,
									NULL,
									(LPBYTE)regStr,
									&dwBufLen);
			if(res == ERROR_SUCCESS){
				if((!_tcscmp(regStr, _T("YES"))) || (!_tcscmp(regStr, _T("yes"))))
					testPool = true;
				else if((!_tcscmp(regStr, _T("NO"))) || (!_tcscmp(regStr, _T("no"))))
					testPool = false;
			}
			else{
				MessageBox(NULL, _T("Twn_MastTracker.dll failed initialization.\nRegisry key \"SOFTWARE\\ABB\\Power Generation Portal\\Tianwan\\Twn_MastTracker\\SampleTime\" ot found."), _T("Error"), MB_OK | MB_ICONERROR);
				return(FALSE);
			}

			dwBufLen = sizeof(regStr);
			res = RegQueryValueEx(	hKey,
									TEXT("TestPoolPage"),
									NULL,
									NULL,
									(LPBYTE)regStr,
									&dwBufLen);
			if(res == ERROR_SUCCESS)
					_stprintf_s(testPoolPage, _T("%s"), regStr);
			else{
				MessageBox(NULL, _T("Twn_MastTracker.dll failed initialization.\nRegisry key \"SOFTWARE\\ABB\\Power Generation Portal\\Tianwan\\Twn_MastTracker\\SampleTime\" ot found."), _T("Error"), MB_OK | MB_ICONERROR);
				return(FALSE);
			}

			RegCloseKey(hKey);
			break;
		default:
			break;
	}
    return TRUE;  // Successful DLL_PROCESS_ATTACH.
}

/********************************************************************************************/
/*																							*/
/*	Function:		MastTracker																*/
/*																							*/
/*	Description:	Activate the automatic page change tracking mast position				*/
/*																							*/
/*	Return:			RETSTAT		-	RT_NORMAL	for success									*/
/*								-	RT_FAILED	otherwise									*/
/*																							*/
/********************************************************************************************/																			
RETSTAT MastTracker(){
	RETSTAT			retSt;
	RETCOND			retCnd;
	GENIDX			zoneIdx;
	GENIDX			mastType;
	ZONENAME		zoneName;
	TAGIDX			tagTVIdx;
	TAGIDX			tiMMMreq;
	TAGIDX			tiMMClusMreq;
	TAGIDX			tiMMFixMreq;
	TAGIDX			tiTrolleyMreq;
	TAGIDX			tiBridgeMreq;
	TAGIDX			tiStart;
	TAGIDX			tiMMRotMreq;
	TAGIDX			tiTVRotMreq;
	TAGIDX			tiBridgePos;
	TAGIDX			tiTrolleyPos;
	TAGVALEX		tagVal;
	TAGVALEX		tagTVVal;
	float			x, y;
	float			falv_x;
	float			falv_y;
	GENIDX			lastZoneIdx;
	bool			h_mov;
	int				pathInsert;
	GENIDX			mainZoneIdx;
	GENIDX			FAZoneIdx;
	GENIDX			HecaZoneIdx;
	PAGNAM			pageName;
	int				opMode;
	bool			movSel;
	bool			clus;
	bool			mast;
	bool			latch;

	lastZoneIdx = 0;

	h_mov = false;
	pathInsert = STEP_INSERT;

	retSt = GetZoneIdx(ZONE_MAIN, &mainZoneIdx);
	retSt = GetZoneIdx(ZONE_FA, &FAZoneIdx);
	retSt = GetZoneIdx(ZONE_HECA, &HecaZoneIdx);
	GetTagIdx(TWN_TV_MOUNT, &tagTVIdx);
	retCnd = GetTagIdx(TWN_FAGR_MREQ, &tiMMMreq);
	retCnd = GetTagIdx(TWN_FAGRL_MREQ, &tiMMFixMreq);
	retCnd = GetTagIdx(TWN_CLGR_MREQ, &tiMMClusMreq);
	retCnd = GetTagIdx(TWN_TROLLEY_MREQ, &tiTrolleyMreq);
	retCnd = GetTagIdx(TWN_BRIDGE_MREQ, &tiBridgeMreq);
	retCnd = GetTagIdx(TWN_START_MOV, &tiStart);
	retCnd = GetTagIdx(TWN_MM_ROT_MREQ, &tiMMRotMreq);
	retCnd = GetTagIdx(TWN_TV_ROT_MREQ, &tiTVRotMreq);
	retCnd = GetTagIdx(TWN_BRIDGE_POS, &tiBridgePos);
	retCnd = GetTagIdx(TWN_TROLLEY_POS, &tiTrolleyPos);

	GetFALVOffset(&falv_x, &falv_y);

	while(1){
		//Se non sono in semiautomatico o automatico aggiorno i dati sui sinottici.//
		retSt = GetDynCurrentMode(&opMode);
		if(retSt != RT_NORMAL)
			goto SLEEP;
		
		//Se non c'� nessun movimento attivo procedo//
		retCnd = TagRead(&tiStart, &tagVal);
		if(retCnd != R_NORMAL)
			goto SLEEP;

		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			retSt = GetMastMounted(&mastType);
			if(retSt != RT_NORMAL)
				goto SLEEP;
			//Controllo se il movimento � orizzontale//
			retCnd = TagRead(&tiBridgeMreq, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;

			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				h_mov = true;
			else{
				retCnd = TagRead(&tiTrolleyMreq, &tagVal);
				if(retCnd != R_NORMAL)
					goto SLEEP;

				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					h_mov = true;
				else{
					retCnd = TagRead(&tiMMRotMreq, &tagVal);
					if(retCnd != R_NORMAL)
						goto SLEEP;

					if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
						h_mov = true;
					else{
						retCnd = TagRead(&tiTVRotMreq, &tagVal);
						if(retCnd != R_NORMAL)
							goto SLEEP;

						if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
							h_mov = true;
						else
							h_mov = false;
					}
				}
			}
				
			if(h_mov){
				switch(mastType){
				case MAIN_MAST:
					retCnd = TagRead(&tiBridgePos, &tagVal);
					if(retCnd != R_NORMAL)
						goto SLEEP;
						
					x = (float)tagVal.value.val_float;
		
					retCnd = TagRead(&tiTrolleyPos, &tagVal);
					if(retCnd != R_NORMAL)
						goto SLEEP;
						
					y = (float)tagVal.value.val_float;
					break;
				case FALC_MAST:
					retCnd = TagRead(&tiBridgePos, &tagVal);
					if(retCnd != R_NORMAL)
						goto SLEEP;
						
					x = tagVal.value.val_float + falv_x;

					retCnd = TagRead(&tiTrolleyPos, &tagVal);
					if(retCnd != R_NORMAL)
						goto SLEEP;
						
					y = (float)tagVal.value.val_float + falv_y;
					break;
				}
				
				if(pathInsert == STEP_INSERT){
					PathStepAdd(mastType, x, y, PATH_TYPE_EXEC);
					pathInsert = 1;
				}
				else
					pathInsert++;
				
				if(!testPool){
					retSt = GetZoneIdxMT(x, y, &zoneIdx);
					if(retSt != RT_NORMAL)
						goto SLEEP;
					if(zoneIdx == lastZoneIdx){
						lastZoneIdx = zoneIdx;
						goto SLEEP;
					}
					else if(zoneIdx == ZONE_C23_IDX){
						_stprintf_s(pageName, _T("P_C23"));
						retCnd = CpSetPage(1, pageName);
						if(retCnd != R_NORMAL)
							goto SLEEP;
						lastZoneIdx = zoneIdx;
					}
					else if(zoneIdx == ZONE_C12_IDX){
						_stprintf_s(pageName, _T("P_C12"));
						retCnd = CpSetPage(1, pageName);
						if(retCnd != R_NORMAL)
							goto SLEEP;
						lastZoneIdx = zoneIdx;
					}
					else if((zoneIdx == mainZoneIdx) || (zoneIdx == FAZoneIdx) || (zoneIdx == HecaZoneIdx))
						goto SLEEP;
					else{
						retSt = GetZoneName(zoneIdx, zoneName);
						if(retSt != RT_NORMAL)
							goto SLEEP;
						if(!_tcscmp(zoneName, ZONE_FA_10))
							_tcscpy_s(zoneName, _T("Z2110"));

						if(!_tcscmp(zoneName, ZONE_IO)){
							retSt = ChangeZ1Page();
							if(retSt != RT_NORMAL)
								goto SLEEP;
						}
						else{
							_stprintf_s(pageName, _T("P_%s"), zoneName);
							retCnd = CpSetPage(1, pageName);
							if(retCnd != R_NORMAL)
								goto SLEEP;
						}
						lastZoneIdx = zoneIdx;
					}
				}
				else{
					if(zoneIdx == lastZoneIdx){
						lastZoneIdx = zoneIdx;
						goto SLEEP;
					}
					retCnd = CpSetPage(1, testPoolPage);
					if(retCnd != R_NORMAL)
						goto SLEEP;
					lastZoneIdx = zoneIdx;
				}
			}
			else{
				zoneIdx = -1 * (mastType + 3);

				if(zoneIdx == lastZoneIdx){
					lastZoneIdx = zoneIdx;
					goto SLEEP;
				}

				if(mastType == MAIN_MAST){
					movSel = false;

					retCnd = TagRead(&tiMMMreq, &tagVal);
					if(retCnd != R_NORMAL)
						goto SLEEP;
					if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
						mast = true;
						clus = false;
						movSel = true;
						latch = false;
					}else{
						retCnd = TagRead(&tiMMClusMreq, &tagVal);
						if(retCnd != R_NORMAL)
							goto SLEEP;
						if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
							movSel = true;
							mast = false;
							clus = true;
							latch = false;
						}else{
							retCnd = TagRead(&tiMMFixMreq, &tagVal);
							if(retCnd != R_NORMAL)
								goto SLEEP;
							if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
								movSel = true;
								mast = false;
								clus = false;
								latch = true;
							}
						}
					}
					
					if(!movSel)
						goto SLEEP;

					if(mast){
						retCnd = TagRead(&tagTVIdx, &tagTVVal);
						if(retCnd != R_NORMAL)
							goto SLEEP;

						if(GtBit((short int)(tagTVVal.value.val_di), 15) == 0){
							_stprintf_s(pageName, _T("MMFAGRMV"));
							retCnd = CpSetPage(1, pageName);
							if(retCnd != R_NORMAL)
								goto SLEEP;
						}
						else{
							_stprintf_s(pageName, _T("MMFAGRMV"));
							retCnd = CpSetPage(1, pageName);
							if(retCnd != R_NORMAL)
								goto SLEEP;
						}
					}else if(clus){
						_stprintf_s(pageName, _T("MMFACLGR"));
						retCnd = CpSetPage(1, pageName);
						if(retCnd != R_NORMAL)
							goto SLEEP;
					}else if(latch){
						_stprintf_s(pageName, _T("MMFAGRLA"));
						retCnd = CpSetPage(1, pageName);
						if(retCnd != R_NORMAL)
							goto SLEEP;
					}

					lastZoneIdx = zoneIdx;
				}
				else if(mastType == FALC_MAST){
					_stprintf_s(pageName, _T("P_FALCD"));
					retCnd = CpSetPage(1, pageName);
					if(retCnd != R_NORMAL)
						goto SLEEP;
					lastZoneIdx = zoneIdx;
				}
				else if(mastType == TV_MAST){
					_stprintf_s(pageName, _T("P_MM+TV_Z-Axis"));
					retCnd = CpSetPage(1, pageName);
					if(retCnd != R_NORMAL)
						goto SLEEP;
					lastZoneIdx = zoneIdx;
				}
			}
		}else
			lastZoneIdx = 0;
SLEEP:
		Sleep(sampleTime);
	}
	return(RT_NORMAL);
}

